﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using NetBay.Core.BusinessEntities;
using NetBay.Core.Log;
using Rio.Framework.Business;
using Rio.Framework.Historical;
#endregion

namespace Rio.Doc.Business.Repository.Translator
{
    /// <summary>
    /// Class used to convert Folder database row to Folder object
    /// </summary>
    public class FolderTranslator
    {
        /// <summary>
        /// Translate a bunch of FolderType DataReader to FolderType object
        /// </summary>
        /// <param name="P_oDbDataReader">The dataReader that contains the rows</param>
        /// <param name="p_lstIndex"></param>
        /// <param name="p_sTableName"></param>
        /// <returns>A list of FolderType Object</returns>
        public static List<Folder> TranslateFolder(IDataReader P_oDbDataReader, List<Index> p_lstIndex, string p_sTableName, bool p_bReportFormat = false)
        {
            var l_lstFolder = new List<Folder>(); 

            try
            {
                using (P_oDbDataReader)
                {
                    while (P_oDbDataReader.Read())
                    {
                        Folder l_oFolder = new Folder();
                        //We populate all other informations on a folder
                        l_oFolder.Identifier = Convert.ToInt32(P_oDbDataReader[p_sTableName + "_NO"].ToString());
                        l_oFolder.Iddoc = P_oDbDataReader[p_sTableName + "_IDDOC"].ToString();
                        l_oFolder.CreationUserIdentifier =
                            Convert.ToInt32(P_oDbDataReader[p_sTableName + "_USERCRE"].ToString());
                        l_oFolder.CreationDate =
                            ConvertField.ToDateTime(P_oDbDataReader[p_sTableName + "_DATECRE"].ToString());
                        l_oFolder.ModificationUserIdentifier =
                            FieldRow.Translate<int>(P_oDbDataReader[p_sTableName + "_USERMOD"].ToString());
                        l_oFolder.ModificationDate =
                            ConvertField.ToDateTime(P_oDbDataReader[p_sTableName + "_DATEMOD"].ToString());
                        l_oFolder.Note = P_oDbDataReader[p_sTableName + "_NOTE"].ToString();
                        l_oFolder.State = P_oDbDataReader[p_sTableName + "_ETAT"].ToString();
                        l_oFolder.Histo = Convert.ToInt32(P_oDbDataReader[p_sTableName + "_HISTO"].ToString());

                        if (!p_bReportFormat)
                        {
                            l_oFolder.NbDoc = FieldRow.Translate<int>(P_oDbDataReader["nb_doc"].ToString());
                            l_oFolder.FolderTypeIdentifier =
                                FieldRow.Translate<int>(P_oDbDataReader["rp_td_id"].ToString());
                            l_oFolder.NbAlert = FieldRow.Translate<int>(P_oDbDataReader["nb_alertes"].ToString());
                        }
                        else
                        {
                            l_oFolder.FolderDocList = P_oDbDataReader["rdocuments"].ToString();
                        }

                        // Foreach folder we loop on the list of all index names to get their values
                        var l_oDicIndexValue = new Dictionary<Index, string>(p_lstIndex.Count);
                        foreach (var l_oIndex in p_lstIndex)
                        {
                            string value = P_oDbDataReader[p_sTableName + "_CH" + l_oIndex.Name].ToString();
                            var index = (Index) l_oIndex.Clone();
                            index.Value = value;
                            l_oDicIndexValue.Add(index, value);
                        }
                        l_oFolder.SetIndexValues(l_oDicIndexValue);
                        l_lstFolder.Add(l_oFolder);
                    }
                }
            }
            catch (Exception ex)
            {
               LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return l_lstFolder;
        }

        /// <summary>
        /// Translate a bunch of FolderType DataReader to FolderType object
        /// </summary>
        /// <param name="P_oDbDataReader">The dataReader that contains the rows</param>
        /// <returns>A list of FolderType Object</returns>
        public static List<Folder> TranslateFolderWithoutIndex(IDataReader P_oDbDataReader)
        {
            var l_lstFolder = new List<Folder>();

            try
            {
                using (P_oDbDataReader)
                {
                    //Loop on all folders
                    while (P_oDbDataReader.Read())
                    {
                        var l_sTableName = P_oDbDataReader["TABLE_NAME"].ToString();
                        var l_oFolder = new Folder();
                        // We populate all other informations on a folder
                        l_oFolder.Identifier = Convert.ToInt32(P_oDbDataReader[l_sTableName + "_NO"].ToString());
                        l_oFolder.Iddoc = P_oDbDataReader[l_sTableName + "_IDDOC"].ToString();
                        l_oFolder.CreationUserIdentifier =
                            Convert.ToInt32(P_oDbDataReader[l_sTableName + "_USERCRE"].ToString());
                        l_oFolder.CreationDate =
                            ConvertField.ToDateTime(P_oDbDataReader[l_sTableName + "_DATECRE"].ToString());
                        l_oFolder.ModificationUserIdentifier =
                            FieldRow.Translate<int>(P_oDbDataReader[l_sTableName + "_USERMOD"].ToString());
                        l_oFolder.ModificationDate =
                            ConvertField.ToDateTime(P_oDbDataReader[l_sTableName + "_DATEMOD"].ToString());
                        l_oFolder.Note = P_oDbDataReader[l_sTableName + "_NOTE"].ToString();
                        l_oFolder.State = P_oDbDataReader[l_sTableName + "_ETAT"].ToString();
                        l_oFolder.Histo = Convert.ToInt32(P_oDbDataReader[l_sTableName + "_HISTO"].ToString());
                        l_oFolder.FolderTypeIdentifier =
                            FieldRow.Translate<int>(P_oDbDataReader["rp_td_id"].ToString());
                        l_oFolder.NbAlert = FieldRow.Translate<int>(P_oDbDataReader["NB_ALERTES"].ToString());


                        l_lstFolder.Add(l_oFolder);
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return l_lstFolder;
        }
    }
}
